package com.boxin.ims.modules.wechat.busi.game;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.busi.BusinessProcessor;
import com.boxin.ims.modules.wechat.busi.ProcessMessage;
import com.boxin.ims.modules.wechat.entity.LotteryLog;
import com.boxin.ims.modules.wechat.entity.LotteryRecord;
import com.boxin.ims.modules.wechat.entity.Option;
import com.boxin.ims.modules.wechat.entity.Question;
import com.boxin.ims.modules.wechat.entity.Questionnaire;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReply;
import com.boxin.ims.modules.wechat.entity.QuestionnaireReplyHuman;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.LotteryLogService;
import com.boxin.ims.modules.wechat.service.QuestionService;
import com.boxin.ims.modules.wechat.service.QuestionnaireReplyHumanService;
import com.boxin.ims.modules.wechat.service.QuestionnaireService;
import com.boxin.ims.modules.wechat.utils.ActivityProcessorUtils;

/**
 * 
 * @author Administrator
 * @date 2014-8-22
 * @function <p> 问题抢答游戏 </p>
 */
@Service
public class AnswerRaceProcessor extends BusinessProcessor{

	@Autowired
	QuestionnaireService questionnaireService;
	
	@Autowired
	QuestionnaireReplyHumanService questionnaireReplyHumanService;
	
	
	@Autowired
	QuestionService questionService;
	
	
	@Autowired
	LotteryLogService lotteryLogService;
	
	
	private static Integer Winners=0;
	
	@Override
	public ProcessMessage process() {
		//判断是否为会员
		WechatMember member= memberCheckAndReply();
		
		
		if(member!=null){
			Questionnaire questionnaire=new Questionnaire();
			questionnaire.setUser(user);
			
			List<Questionnaire> questionnaireList = questionnaireService.findByStartup(questionnaire);
			
			if(questionnaireList.size()==0){
				writeToUser("抢答还未开启，您准备好了没!!");
				return null;
			}
			
			questionnaire=questionnaireList.get(0);
			
				if("1".equals(questionnaire.getType())){
					
					if("0".equals(questionnaire.getIsRepeat())){
						List<QuestionnaireReplyHuman> humanList= questionnaireReplyHumanService.getByWechatId(member.getId(), questionnaire.getId());
						if(humanList.size()>0){
							writeToUser("您已经抢答过了，请期待下一轮");
							return null;
						}
					}
				}
				
				
				List<Question> questionList=questionService.findByQuestionnaireId(questionnaire.getId());
				
				
				//保存中奖人信息
				QuestionnaireReplyHuman replyHuman=new QuestionnaireReplyHuman();
				replyHuman.setQuestionnaire(questionnaire);
				replyHuman.setName(member.getName()  );
				replyHuman.setMobile(member.getMobile());
				replyHuman.setWechatMember(member);
				replyHuman.setWechatId(member.getWechatId());
				questionnaireReplyHumanService.save(replyHuman);
				
				
				String answer="";
				
				for (int i = 0; i < questionList.size(); i++) {
					answer+=questionList.get(i).getAnswer();
				}
				
				if(questionnaire.getActivityConfig().getLotteryNumber()<=0){
					writeToUser("您的手慢了一步，很遗憾啊！");
					return null;
				}
				String upon=textMessage.getContent().toLowerCase();
				
				//判断中奖
				Map<String, Object> param =new HashMap<String, Object>();
				param.put("questionnaire",questionnaire );
				
				LotteryLog lotteryLog =new LotteryLog();
				
				if(upon.equals(answer.toLowerCase())){
					Integer count=0;
					List<LotteryLog> lotteryLogList=  lotteryLogService.findByMemberIdAndDate(questionnaire.getUser().getId(), member.getId(),DateUtils.getDayStartTime());
					for (int i = 0; i <lotteryLogList.size(); i++) {
						String str="活动处理器奖励_"+questionnaire.getId();
						if(str.equals(lotteryLogList.get(i).getRemarks())){
							count++;
						}
					}
					if(count==0){
						LotteryRecord lotteryRecord=  ActivityProcessorUtils.startAnserActivityProcessor(member, questionnaire.getActivityConfig(), param);
						//奖品为空  添加奖品失败或者奖品次数用完
						if(lotteryRecord==null){
							writeToUser("您的手慢了一步，很遗憾啊！");
							return null;
						}
						writeImageToUser("抢答奖品二维码", "恭喜您获得"+lotteryRecord.getName()+"请您到指定地点领取！", lotteryRecord.getQrCode().getId());
						return null;
					}
					writeToUser("您已经得到奖品了快去会员中心看看吧!!");
					return null;
					
				}
				
				lotteryLog.setUser(user);
				lotteryLog.setMember(member);
				lotteryLog.setName( questionnaire.getActivityConfig().getLottery().getName());
				lotteryLog.setRemarks("抢答，答题错误没有获得奖励");
				
				lotteryLogService.save(lotteryLog);
				
				writeToUser("很遗憾您没有答对！！");
			
		}
		
		
		
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		
		return false;
	}
	
	public void clear(){
		
	}
	
	
	

}
